﻿using EyFramework.Common;
using EyFramework.Library;
using Ncov2019.Core.Service;
using Ncov2019.Entity;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace Ncov2019
{
    public partial class FrmNcovLabConfirm : FrmDevBase
    {
        private List<GridColumnInfo> columnInfos = new List<GridColumnInfo>();

        private INcovBarConfirmService ncovBarConfirmService;

        private List<NcovBarConfirm> ncovBarConfrims;

        public FrmNcovLabConfirm()
        {
            InitializeComponent();
        }

        private void FrmNcovLabConfirm_Load(object sender, EventArgs e)
        {
            dancai.Checked = true;
            hunjian.Checked = false;
            ncovBarConfirmService = Globals.GetService<INcovBarConfirmService>();
            InitForm();
            teBar.Focus();
            QueryData();
        }

        private void InitForm()
        {
            lcNcovArea.Text = Globals.GetSysConfig("NcovCollectArea", "未配置当前采集窗口");
            gvData.ShowRowNum();
            gcData.ShowContextMenu(this.components);//显示右键导出功能
            #region
            columnInfos.Add(new GridColumnInfo("BINGRENID", "病历号码"));
            columnInfos.Add(new GridColumnInfo("XINGMING", "姓名"));
            columnInfos.Add(new GridColumnInfo("CAIJISJ", "采集日期")
            {
                Formattype = DevExpress.Utils.FormatType.DateTime,
                Formatstring = "yyyy-MM-dd HH:mm:ss"
            }); ;
            columnInfos.Add(new GridColumnInfo("CAIJIR", "采集人"));
            columnInfos.Add(new GridColumnInfo("TIAOMAHAO", "条码号"));
            columnInfos.Add(new GridColumnInfo("CAIJIQY", "采集区域"));
            #endregion
        }

        private void QueryData()
        {
            NcovBarConfirmParam ncovBarConfirmParam = new NcovBarConfirmParam()
            {
                CAIJIR = Globals.CurrentUser.StaffName,
                CAIJIRQ = ncovBarConfirmService.GetCurrentDate("YYYY-MM-DD")
            };
            ncovBarConfrims = ncovBarConfirmService.queryTodayData(ncovBarConfirmParam);
            gcData.DataSource = ncovBarConfrims;
            lblLabCount.Text = ncovBarConfrims.Count.ToString();
            DevPublic.SetGridControlView(gvData, columnInfos, true, true);
        }

        private void ScanBar(string barCode)
        {
            if (lcNcovArea.Text == "未配置当前采集窗口")
            {
                Globals.ShowError("当前窗口未配置采集窗口，请到配置文件Applation.ini文件中配置NcovCollectArea");
                return;
            }
            NcovBarInfoDto ncovBarInfo = ncovBarConfirmService.GetNcovBarInfo(barCode);
            if (ncovBarInfo == null)
            {
                Globals.ShowError("未找到当前条码的信息！");
                return;
            }
            else
            {
                if (ncovBarInfo.JIANYANXMMC.IndexOf("COVID-19") < 0)
                {
                    Globals.ShowError(string.Format("当前扫码的是《{0}》的条码，请确认条码是否正确！", ncovBarInfo.JIANYANXMMC));
                    teBar.SelectAll();
                    return;
                }
            }
            AddBar(ncovBarInfo);
        }

        private void AddBar(NcovBarInfoDto ncovBarInfo)
        {
            NcovBarConfirm ncovBarConfirm = new NcovBarConfirm();
            ncovBarConfirm.BINGRENID = ncovBarInfo.BINGRENID;
            ncovBarConfirm.XINGMING = ncovBarInfo.XINGMING;
            ncovBarConfirm.TIAOMAHAO = ncovBarInfo.TIAOMAHAO;
            ncovBarConfirm.CAIJISJ = Convert.ToDateTime(ncovBarConfirmService.GetCurrentDate());
            ncovBarConfirm.CAIJIRQ = ncovBarConfirm.CAIJISJ.Value.ToString("yyyy-MM-dd");
            ncovBarConfirm.CAIJIR = Globals.CurrentUser.StaffName;
            ncovBarConfirm.CAIJIQY = lcNcovArea.Text;
            if (dancai.Checked)
            {
                ncovBarConfirm.JIANYANFS = "单采";
            }
            else {
                ncovBarConfirm.JIANYANFS = "混采";
            }
            //先判断是否已经扫码登记过该条码
            if (ncovBarConfirmService.Exists(ncovBarConfirm))
            {
                Globals.ShowWarning("当前条码已经被登记，请勿重复扫码！");
                return;
            }
            if (ncovBarConfirmService.AddNcovBarConfirm(ncovBarConfirm))
            {
                ncovBarConfrims.Add(ncovBarConfirm);
                lblTmh.Text = ncovBarConfirm.TIAOMAHAO;
                lblXm.Text = ncovBarConfirm.XINGMING;
                lblLabCount.Text = ncovBarConfrims.Count.ToString();
            }
            else
            {
                Globals.ShowError("保存扫码记录失败");
            }
            gvData.RefreshData();
            teBar.Text = "";
            teBar.Focus();
        }

        private void teBar_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13 && !string.IsNullOrEmpty(teBar.Text.Trim()))
            {
                ScanBar(teBar.Text.Trim());
            }
        }

        private void gcData_DoubleClick(object sender, EventArgs e)
        {
            if (gvData.FocusedRowHandle >= 0)
            {
                NcovBarConfirm ncovBarConfirm = gvData.GetRow(gvData.FocusedRowHandle) as NcovBarConfirm;
                if (Globals.ShowYesNoAndTips(string.Format("您正在删除已经姓名：【{0}】,条码号：【{1}】的确认标本，是否继续删除！",
                    ncovBarConfirm.XINGMING, ncovBarConfirm.TIAOMAHAO)) == DialogResult.Yes)
                {
                    ncovBarConfirmService.Delete(ncovBarConfirm.CONFIRM_SEQ);
                    QueryData();
                }
            }
        }
    }
}